﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Diagnostics;
using Debug = UnityEngine.Debug;

//WaitUntil
//直到达到***才执行
//等到指定对象的参数发生变化时   参数目标 判断方法的委托  如果返回值变化的话 就发生变化

//WaitUntilValueChanged
//等到指定对象的参数发生变化时，才完成。

public class Two : MonoBehaviour
{
    public GameObject ball;
    void Start()
    {
        WaitUtilTest();
        WaitUntilValueTest();
    }

    // Update is called once per frame
    void Update()
    {
        ball.transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime);
    }

    async void WaitUtilTest()
    {
        Debug.Log("Start" + Time.time);
        await UniTask.WaitUntil(() => isFarThanOne());
        Debug.Log("End" + Time.time);
        ball.GetComponent<Renderer>().material.color = Color.red;
    }

    public bool isFarThanOne()
    {
        if (ball.transform.position.x > 1)
        // if (ball.transform.position.x < -1)
        {
            return true;
        }
        return false;
    }

    async void WaitUntilValueTest()
    {
        await UniTask.WaitUntilValueChanged(ball.transform, x => x.position);
        Debug.Log("小球值变化");
    }
}
